
;;===================================================================
;; Script which plots the locations of the 113 USETHESE_RB3 cores
;;   atop the GLIMPSE 8um maps.  These are to be used for
;;   corroborating the results of the Near/Far output from the IRDC
;;   modeling process (model_ffore.pro).


;; For each core, look at the image, including (l,b) of Rathborne
;; core, and TVELLIPSE of associated IRDC.


restore,'./irdc_dist_model/usethese_rb3.sav',/ver
s = read_bgps_csv('bgps_distance_database.csv',csv,/ver)
POST_DIR='./local/postage/'

nl3 = n_elements(rb3)
lastfn = ''

set_plot,'ps'
!p.font=0

beam_min = fltarr(nl3)
img_val = fltarr(nl3)

cloudname = ''
FOR i=0L, nl3-1 DO BEGIN
   
   pcnam = '....'
   
   IF rb3[i].name NE cloudname THEN BEGIN
      cloudname = rb3[i].name
      message,'Press Enter for next R06 cloud...',/inf
      ;;junk = get_kbrd()
      
      gfn = './local/glimpse_v3/GLM_'+$
            string(round(rb3[i].cl/3.)*3,format="(I03)")+$
            '00+0000_mosaic_I4.fits'
      
      IF gfn NE lastfn THEN BEGIN
         print,gfn
         gimage = readfits(gfn,hdr)
         gimage *= 0.8d   ;; Calibration factor needed (See PF09)
         extast,hdr,glastr
         glsize=size(gimage,/DIM)
         lastfn=gfn
      ENDIF   
      
      
      ;; Get 15' x 15' subimage centered at (cl,cb)
      ad2xy,rb3[i].cl,rb3[i].cb,glastr,x,y
      cent = round([x,y])
      subsize = long(round( [30.,30.]/60. / glastr.cd[1,1] + 1.))
      start = cent - round((subsize - 1.)/2.)
      ;; As part of this... need to associate WCS with new subsection...
      ;; Check starting and stopping pixels to ensure in range...
      ind = WHERE(start LT 0, nind)
      IF nind NE 0 THEN start[ind] = 0
      ind = WHERE(start+subsize GT glsize, nind)
      IF nind NE 0 THEN BEGIN
         excess = ((start + subsize) - glsize)>0
         subsize -= excess
      ENDIF
      
      ;; Use HEXTRACT to extract the subsection, and update header WCS
      hextract,gimage,hdr,ps,pshdr,start[0],start[0]+subsize[0]-1,$
               start[1],start[1]+subsize[1]-1,/silent


      device,/close_file
      
      mydevice,filename='local/mir_studies/ut_'+rb3[i].name+'.eps',ct=0,$
               xsize=10,ysize=10
      
      plotimage,alog(ps),range=set_plot_range(alog(ps)),$
                /preserve,xst=4,yst=4,title=rb3[i].name
      plot_wcs_axes,pshdr
      
      pcnam = cloudname
   ENDIF
   
   ;; Set colors
   IF !p.font EQ -1 THEN BEGIN
      c_mms = 'ffff00'x
      c_ell = '8728F5'x
   ENDIF ELSE BEGIN
      cgLoadct,18,/silent
      c_mms = 150
      c_ell = 40
   ENDELSE
   
   cs = 0.8
   ;; Plot R06 core location, plus nearest IRDC ellipse
   plots,rb3[i].l,rb3[i].b,psym=7,color=c_mms,thick=2
   
   ;; Get BGPS information
   gcirc, 2, rb3[i].l, rb3[i].b, s.glon_peak, s.glat_peak, dis
   mind = MIN(dis, bind)
   IF mind LE 60. THEN BEGIN
      cnum = string(s[bind].cnum,format="(I04)")
      bgps_labl = readfits(POST_DIR+'label'+cnum+'.fits',lhdr,/silent)
      plot_wcs_axes,lhdr,10,LONARR=blarr,LATARR=bbarr
      contour,bgps_labl,blarr,bbarr,levels=[0.5],thick=4,color=200,/overplot
   ENDIF
   
   tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
             -rb3[i].ir_pa,/data,thick=1.5,color=c_ell
   al_legend,[rb3[i].core],position=[rb3[i].l,rb3[i].b+.005],$
             textcolor=[c_mms],box=0,charsize=cs
   al_legend,[rb3[i].core],position=[rb3[i].ir_l,rb3[i].ir_b],$
             textcolor=[c_ell],box=0,charsize=cs
   
   
ENDFOR

device,/close_file
!p.font=-1
set_plot,'x'


END
